package com.qf.factory;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.FactoryBean;

import java.io.InputStream;

/**
 * 自定义工厂
 * FactoryBean:Spring容器的扩展工厂
 * @author lixu
 */
public class MyFactory implements FactoryBean {
    /**
     * 根据ID获取的对象
     * @return
     * @throws Exception
     */
    @Override
    public Object getObject() throws Exception {
        //1：准备流
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        //2：Mybatis的工厂 （23种设计模式中工厂模式）
        SqlSessionFactory sqlSessionFactory =  new SqlSessionFactoryBuilder().build(inputStream);
        return sqlSessionFactory;
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }

}
